using System.Collections;
using UnityEngine;

public class IntroScreen : MonoBehaviour
{
	public UISprite background;

	public UITexture logo;

	public UIWidget canContinueWidget;

	public IntroButton introButton;

	protected bool canContinue;

	public Vector3 offsetBackground;

	public Vector3 endScale;

	public float fadeOutDuration = 0.5f;

	public float fadeInDuration = 1.5f;

	public float delayTillTapToContinue = 0f;

	private void OnEnable()
	{
		Debug.Log("Intro screen");
		introButton.Init(OnContinue);
		TableTennisMainNavigation instance = TableTennisMainNavigation.instance;
		instance.SetHeader(TableTennisMainNavigation.HeaderType.None);
		instance.characterView.ShowAll();
		instance.characterView.ShowView(CharacterDisplayView.ViewType.IntroView, false);
		Time.timeScale = 1f;
		StopAllCoroutines();
		StartCoroutine(ShowIntroSequence());
	}

	private IEnumerator ShowIntroSequence()
	{
		canContinue = false;
		canContinueWidget.cachedGameObject.SetActive(false);
		yield return StartCoroutine(FadeFromTo(background, 1f, 0f, fadeInDuration));
		yield return new WaitForSeconds(delayTillTapToContinue);
		canContinueWidget.cachedGameObject.SetActive(true);
		canContinue = true;
	}

	private IEnumerator FadeFromTo(UIRect widget, float from, float to, float duration)
	{
		float time = 0f;
		//while (time < duration)
		//{
		//	time += RealTime.deltaTime;
		//	float normTime = time / duration;
		//	//widget.alpha = Mathf.Lerp(from, to, normTime);
		//	yield return null;
		//}
		yield return null;
		//widget.alpha = to;
	}

	private IEnumerator MoveFromTo(UIRect widget, Vector3 from, Vector3 to, float duration)
	{
		//float time = 0f;
		//widget.cachedTransform.localPosition = from;
		//while (time < duration)
		//{
		//	time += RealTime.deltaTime;
		//	float normTime = time / duration;
		//	widget.cachedTransform.localPosition = Vector3.Lerp(from, to, normTime);
		//	yield return null;
		//}
		//widget.cachedTransform.localPosition = to;
		yield return null;
	}

	private IEnumerator ScaleFromTo(UIRect widget, Vector3 from, Vector3 to, float duration)
	{
		float time = 0f;
		widget.cachedTransform.localScale = from;
		while (time < duration)
		{
			time += RealTime.deltaTime;
			float normTime = time / duration;
			widget.cachedTransform.localScale = Vector3.Lerp(from, to, normTime);
			yield return null;
		}
		widget.cachedTransform.localScale = to;
	}

	public void OnContinue()
	{
		if (canContinue)
		{
			StopAllCoroutines();
			canContinue = false;
			StartCoroutine(ExitToCustomization());
		}
	}

	private IEnumerator ExitToCustomization()
	{
		GGPlayerSettings player = GGPlayerSettings.instance;
		TableTennisMainNavigation mainNav = TableTennisMainNavigation.instance;
		CustomisationSelectLayer.CustomisationView activeView = mainNav.customisation.GetActiveViewInfo();
		//mainNav.characterView.ShowView(activeView.cameraTransform.position, activeView.cameraTransform.rotation, activeView.settings, true);
		Vector3 fromMove = logo.cachedTransform.localPosition;
		Vector3 toMove = fromMove + offsetBackground;
		Vector3 fromScale = logo.cachedTransform.localScale;
		Vector3 toScale = endScale;
		StartCoroutine(MoveFromTo(logo, fromMove, toMove, fadeOutDuration));
		StartCoroutine(ScaleFromTo(logo, fromScale, toScale, fadeOutDuration));
		yield return StartCoroutine(FadeFromTo(GetComponent<UIPanel>(), 1f, 0f, fadeOutDuration));
		if (!player.Model.shownMainMenuTutorial)
		{
			mainNav.ReplaceOnTop(mainNav.customisation.gameObject);
		}
		else
		{
			mainNav.Pop();
		}
	}
}
